package org.fms.web.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.fms.web.mapper.AnnexMapper;
import org.fms.web.mapper.RegistrationMapper;
import org.fms.web.mapper.UserMapper;
import org.fms.web.model.Annex;
import org.fms.web.model.Registration;
import org.fms.web.model.User;
import org.fms.web.service.OperatorService;
import org.fms.web.service.vo.AnnexVO;
import org.fms.web.service.vo.PageResultVO;
import org.fms.web.service.vo.RegistrationVO;
import org.fms.web.service.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * PACKAGE     : org.fms.web.service.impl
 * CREATE DATE : 2018-03-03
 * AUTHOR      : xiaolu.fan
 * 文件描述     :业务操作类
 */
@Service
public class OperatorServiceImpl extends ServiceImpl<RegistrationMapper,Registration> implements OperatorService {

    private Logger logger = LoggerFactory.getLogger(OperatorServiceImpl.class);

    @Resource
    private RegistrationMapper registrationMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AnnexMapper annexMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;


    @Override
    public Map getBasicInfo(Integer id) {
        logger.error("id", id);
        logger.info(registrationMapper == null ? null : registrationMapper.toString());
//        logger.info(registrationMapper.selectAll() == null ? null : registrationMapper.selectAll().toString());
        return null;
    }

    @Override
    public PageResultVO getAllByPageList(Integer pageNumber, Integer pageSize) {
        Page page = PageHelper.startPage(pageNumber, pageSize);
        List<Registration> registrationList = registrationMapper.selectAll();
        long total = page.getTotal();
        PageResultVO pageResultVO = PageResultVO.getPagedResult(registrationList, total, page.getPageNum(), page.getPages(), page.getPageSize());
        return pageResultVO;
    }

    @Override
    public RegistrationVO getDetailData(Integer id) {
        if (id == null) {
            return null;
        }
        List<Annex> annexList = annexMapper.selectByRegistrationId(id);
        Registration registration = registrationMapper.selectById(id);
        List<AnnexVO> annexVOList = new ArrayList<>();
        for (Annex annex : annexList) {
            AnnexVO annexVO = new AnnexVO();
            BeanUtils.copyProperties(annex, annexVO);
            annexVOList.add(annexVO);
        }
        RegistrationVO registrationVO = new RegistrationVO();
        BeanUtils.copyProperties(registration, registrationVO);
        registrationVO.setAnnexList(annexVOList);
        return registrationVO;
    }

    @Override
    public int saveUser(UserVO userVO) {
        User user = new User();
        if (userVO != null) {
            BeanUtils.copyProperties(userVO, user);
            userMapper.insert(user);
            return 0;
        } else {
            return -1;
        }
    }

    @Override
    public int saveAnnex(AnnexVO annexVO) {
        Annex annex = new Annex();
        if (annexVO == null) {
            return -1;
        }
        BeanUtils.copyProperties(annexVO, annex);
        annexMapper.insert(annex);
        return 0;
    }

    @Override
    public int saveRegistration(RegistrationVO registrationVO) {
        Registration registration = new Registration();
        BeanUtils.copyProperties(registrationVO, registration);
        Integer value = registrationMapper.insert(registration);
        logger.error("primary key:" + registration.getId());
        return value;
    }

    @Override
    public int delete(Integer id) {
        return registrationMapper.deleteRegistration(id);

    }

    @Override
    public int save(RegistrationVO registrationVO) {
        Registration registration = new Registration();
        BeanUtils.copyProperties(registrationVO,registration);
        int result = this.baseMapper.updateRegistrationById(registration);
        return result;
    }
}